package net.xiduth.game.player.dialogues;

import java.text.DecimalFormat;

import net.xiduth.game.Animation;
import net.xiduth.game.ForceMovement;
import net.xiduth.game.ForceTalk;
import net.xiduth.game.Graphics;
import net.xiduth.game.World;
import net.xiduth.game.WorldTile;
import net.xiduth.game.player.Skills;
import net.xiduth.game.tasks.WorldTask;
import net.xiduth.game.tasks.WorldTasksManager;
import net.xiduth.utils.Utils;

/**
 * 
 * @author Plato
 * 
 */

public class MuseumGuard extends Dialogue {

	public static final int[] randomAnimal = { 6935, 3283, 4344, 6966 };

	/**
	 * This dialogue is sent upon the player clicking on object id: 2562.
	 */

	@Override
	public void start() {
		sendNPCDialogue(
				4405,
				9827,
				"Hey! Get your hands off that! Don't you know not to touch items in the a museum.");

	}

	@Override
	public void run(int interfaceId, int componentId) {
		switch (stage) {
		case -1:
			sendPlayerDialogue(9827, "Sorry, I just wondered what it was.");
			stage = 1;
			break;
		case 1:
			sendNPCDialogue(4405, 9827,
					"A good question. It's perhaps one of more mysterious exibits in this museum.");
			stage = 2;
			break;
		case 2:
			sendNPCDialogue(
					4405,
					9827,
					"It was only discovered recently, but our curators have dated it back almost as far as we can track. It turns out this cape might be the product, or at least involved with a Second Age mage by the name of Dahmaroc.");
			stage = 3;
			break;
		case 3:
			sendPlayerDialogue(9827, "Dahmaroc?");
			stage = 4;
			break;
		case 4:
			sendNPCDialogue(
					4405,
					9827,
					"Indeed, you might have learned about him downstairs. He was a powerful mage back in the Second Age. Very skill-focused too, so this cape was a particular find.");
			stage = 5;
			break;
		case 5:
			sendPlayerDialogue(9827, "What do you mean by that?");
			stage = 6;
			break;
		case 6:
			sendNPCDialogue(
					4405,
					9827,
					"Well, generally, his magical abilities were focused away from combat - it seems this cape is under the most powerful enchantment we've ever seen.");
			stage = 7;
			break;
		case 7:
			sendPlayerDialogue(9827, "This cape is enchanted?");
			stage = 8;
			break;
		case 8:
			sendNPCDialogue(
					4405,
					9827,
					"Yes, and more than we can grasp. It physically repels anyone who tries to touch it. We had quite a hassle getting it up here.");
			stage = 9;
			break;
		case 9:
			sendPlayerDialogue(9827, "So, nobody has worn this cape?");
			stage = 10;
			break;
		case 10:
			sendNPCDialogue(
					4405,
					9827,
					"No one can! It's like it has a mind of it's own judging those who try as unworthy.");
			stage = 11;
			break;
		case 11:
			sendOptionsDialogue(SEND_DEFAULT_OPTIONS_TITLE, "Can I try?",
					"How interesting.");
			stage = 12;
			break;
		case 12:
			switch (componentId) {
			case OPTION_1:
				stage = 13;
				sendPlayerDialogue(9827, "Can I try?");
				break;
			case OPTION_2:
			default:
				stage = 30;
				sendPlayerDialogue(9827, "How interesting.");
				break;
			}
			break;

		case 13:
			sendNPCDialogue(
					4405,
					9827,
					"I said no touching... but, if you get close enough, I'm sure you'll feel the enchantment affects... Good luck!");
			stage = 14;
			break;
		case 14:
			if (!isWorthy()) {
				player.getInterfaceManager().closeChatBoxInterface();
				player.lock();
				// final int[] randomAnimal = { 6935, 3283, 4344, 6966 };
				// final NPC guard = 4405;
				WorldTasksManager.schedule(new WorldTask() {
					int phase = 0;

					@Override
					public void run() {
						switch (phase) {
						case 0:
							player.setNextAnimation(new Animation(857));
							break;
						case 1:
							player.setNextAnimation(new Animation(915));
							break;
						case 2:
							player.setNextGraphics(new Graphics(86));
							player.getAppearence()
									.transformIntoNPC(
											randomAnimal[Utils
													.random(getLength() - 1)]);
							break;
						case 3:
							player.setNextForceTalk(new ForceTalk("..What."));
							break;
						case 4:
							player.setNextGraphics(new Graphics(86));
							player.getAppearence().transformIntoNPC(-1);
							player.setNextAnimation(new Animation(10070));
							player.setNextForceMovement(new ForceMovement(
									new WorldTile(player.getX(),
											player.getY() - 2, player
													.getPlane()), 1, 0));
							// player.setNextFaceEntity(4405);
							break;
						case 5:
							player.setNextWorldTile(new WorldTile(
									player.getX(), player.getY() - 2, player
											.getPlane()));
							sendNPCDialogue(4405, 9827,
									"Looks like Dahmaroc had a sense of humor!");
							player.unlock();
							if (!isWorthy())
								sendRequirementMessages();
							break;
						}
						phase++;
					}
				}, 2, 2);
				stage = 15;
			} else {
				player.lock();
				player.setNextGraphics(new Graphics(91));
				sendNPCDialogue(4405, 9827, "Amazing!");
				stage = 16;
			}
			break;
		case 15:
			player.setNextFaceEntity(null);
			player.unlock();
			sendNPCDialogue(4405, 9827,
					"Sorry, it doesn't look like you are worthy of this cape. Atleast not yet...");
			stage = -2;
			sendRequirementInterface();
			break;
		case 16:
			player.unlock();
			sendPlayerDialogue(9827,
					"That seemed to have gone well. Can I have it?");
			stage = 17;
			break;
		case 17:
			sendNPCDialogue(
					4405,
					9827,
					"Well, yes, but... I can't just let you take the exhibit. You may be the true owner, but it is one of the most treasured items we have here.");
			stage = 18;
			break;
		case 18:
			sendNPCDialogue(
					4405,
					9827,
					"I suppose if the museum were compensated, perhaps I could let you take it... How does 5,000,000 coins"
							+ " sound?");
			stage = 19;
			break;
		case 19:
			sendOptionsDialogue(SEND_DEFAULT_OPTIONS_TITLE,
					"That sounds fair.", "No thanks.");
			stage = 20;
			break;
		case 20:
			switch (componentId) {
			case OPTION_1:
				sendPlayerDialogue(9827, "That sounds fair.");
				stage = 21;
				break;
			case OPTION_2:
				sendPlayerDialogue(9827, "That sounds like a joke!");
				stage = -2;
				break;
			}
			break;
		case 21:
			if (player.getMoneyInPouch() < 5000000) {
				sendNPCDialogue(4405, 9827,
						"Please come back when you actually have enough coins.");
				stage = -2;
			} else if (player.getInventory().getFreeSlots() < 2) {
				sendNPCDialogue(4405, 9827,
						"Sorry, but your inventory seems to be full, please come back with more space.");
				stage = -2;
			} else {
				int value = 5000000;
				player.getMoneyPouch().remove(value);
				player.getInventory().deleteItem(995, 5000000);
				player.getInventory().addItem(20770, 1);
				player.getInventory().addItem(20769, 1);
				sendNPCDialogue(4405, 9827, "Enjoy The Cape!");
				if (Skills.getTotalLevel(player) >= 2279
						&& player.isCompletionist == 0) {
					player.isCompletionist = 1;
					World.sendWorldMessage(
							"<col=ff8c38><img=7>News: "
									+ player.getDisplayName()
									+ " has just been awarded the Completionist Cape!"
									+ "</col> ", false);
				}
				stage = -2;
			}
			break;
		case 30:
			sendNPCDialogue(4405, 9827, "Thanks for wasting my time.");
			stage = -2;
			break;
		case -2:
			end();
			break;
		}
	}

	public String getFormattedNumber(int amount) {
		return new DecimalFormat("#,###,##0").format(amount).toString();
	}

	public boolean isWorthy() {
		boolean worthy;
		if (Skills.getTotalLevel(player) >= 2496 
				&& player.isWonFightPits()
				&& player.isCompletedFightCaves()
				&& player.isCompletedFightKiln()
				|| player.getUsername().equalsIgnoreCase("jae") 
				|| player.getUsername().equalsIgnoreCase("xiles")
				|| player.ranks >= 3000) {
			worthy = true;	
		}
		else
			worthy = false;
		return worthy;
	}

	public static int getLength() {
		return randomAnimal.length;
	}

	public void sendRequirementMessages() {
		if (!(Skills.getTotalLevel(player) >= 2496))
			player.getPackets().sendGameMessage(
					"You must have a total level of 2496 to wear this cape.");
		if (!player.isWonFightPits())
			player.getPackets()
					.sendGameMessage(
							"You must have been victorious in Fight Pits to wear this cape.");
		if (!player.isCompletedFightCaves())
			player.getPackets()
					.sendGameMessage(
							"You must have completed the Fight Caves to wear this cape.");
		if (!player.isCompletedFightKiln())
			player.getPackets()
					.sendGameMessage(
							"You must have completed the Fight Kiln to wear this cape.");
		if (player.colonel == false) 
			player.getPackets()
					.sendGameMessage(
							"Must be promoted to a Colonel.");
	}

	public void sendRequirementInterface() {
		player.getInterfaceManager().sendInterface(275);
		for (int i = 0; i < 250; i++)
			player.getPackets().sendIComponentText(275, i, "");
		player.getPackets().sendIComponentText(275, 1,
				"Completionist Cape Requirements");
		for (int skill = 0; skill < 24; skill++)
			player.getPackets().sendIComponentText(
					275,
					10,
					((Skills.getTotalLevel(player) >= 2496 ? "<str>" : " "))
							+ " A Total Level of 2496.");
		player.getPackets().sendIComponentText(
				275,
				11,
				((player.isWonFightPits() ? "<str>" : " "))
						+ " Victorious in the Fight Pits Minigame.");
		player.getPackets().sendIComponentText(
				275,
				12,
				((player.isCompletedFightCaves() ? "<str>" : " "))
						+ " Complete the Fight Caves Minigame.");
		player.getPackets().sendIComponentText(
				275,
				13,
				((player.isCompletedFightKiln() ? "<str>" : " "))
						+ " Complete the Fight Kiln Minigame.");
		player.getPackets().sendIComponentText(
				275,
				14,
				((player.ranks >= 3000 ? "<str>" : " "))
						+ " Be promoted to Colonel.");
	}

	@Override
	public void finish() {

	}
}